package com.hdu.mockService.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class RandomDateTimeGenerator {

    public static void main(String[] args) {
        String startTimeStr = "2023/01/01 00:00:00";
        String endTimeStr = "2023/12/31 23:59:59";

        // yyyy-MM-dd HH:mm:ss
        LocalDateTime randomDateTime = getRandomDateTime(startTimeStr, endTimeStr, "yyyy/MM/dd HH:mm:ss");
        System.out.println(randomDateTime);
    }

    public static LocalDateTime getRandomDateTime(String startTimeStr, String endTimeStr, String pattern) {
        LocalDateTime startDateTime = parseDateTime(startTimeStr, pattern);
        LocalDateTime endDateTime = parseDateTime(endTimeStr, pattern);

        return generateRandomDateTime(startDateTime, endDateTime);
    }

    private static LocalDateTime parseDateTime(String dateTimeStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateTimeStr, formatter);
    }

    private static LocalDateTime generateRandomDateTime(LocalDateTime start, LocalDateTime end) {
        long startTimestamp = start.toInstant(ZoneOffset.UTC).toEpochMilli();
        long endTimestamp = end.toInstant(ZoneOffset.UTC).toEpochMilli();

        long randomTimestamp = startTimestamp + (long) (Math.random() * (endTimestamp - startTimestamp));

        return LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(randomTimestamp), ZoneId.systemDefault());
    }
}
